<template>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
            <el-input v-model="ruleForm.title" :disabled="formDisabled"></el-input>
        </el-form-item>
        <el-form-item label="上传封面" prop="display">
            <el-upload class="avatar-uploader" :action="uploadfileurl" :show-file-list="false" accept=".jpg,.jpeg,.png"
                ref="upload" :on-success="success" :auto-upload="true" :before-upload="beforeUpload"
                :disabled="formDisabled">
                <img v-if="ruleForm.display" :src="ruleForm.display" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item label="内容" v-if="formDisabled == false">
            <div id="div2" class="edit"></div>
        </el-form-item>
        <el-form-item label="内容" v-else>
            <div class="edit-cont" v-html="ruleForm.cont"></div>
        </el-form-item>
    </el-form>
</template>

<script>
    import {
        httpAction,
        getAction
    } from '@/http/axios.js';
    import {
        JVxeTableModelMixin
    } from '@/utils/JVxeTableModelMixin.js'

    export default {
        name: 'NewsForm',
        mixins: [JVxeTableModelMixin],
        data() {
            return {
                rules: {
                    title: [{
                        required: true,
                        message: '请输入标题',
                        trigger: ['change', 'blur']
                    }],
                    display: [{
                        required: true,
                        message: '请上传封面',
                        trigger: ['change', 'blur']
                    }],
                },
                url: {
                    add: "/information/add",
                    edit: "/information/modify"
                }
            }
        },
        props: {
            //表单禁用
            disabled: {
                type: Boolean,
                default: false,
                required: false
            }
        },
        computed: {
            formDisabled() {
                return this.disabled
            },
        },
        created() {},
        methods: {
            /** 确定按钮点击事件 */
            submitForm() {
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        this.ruleForm.cont = this.editor.txt.html()
                        this.ruleForm.token = this.$store.state.token
                        let url = this.url.add
                        if (this.ruleForm.id) {
                            url = this.url.edit
                            /* ------后台需要支持所有参数修改---------- */
                            /* ------不然没法统一封装---------- */
                            delete this.ruleForm.pageNum
                            delete this.ruleForm.pageSize
                            delete this.ruleForm.createuser
                            delete this.ruleForm.createusername
                            delete this.ruleForm.createtime
                        }
                        this.confirmLoading = true
                        httpAction(url, this.ruleForm).then(res => {
                            console.log(res);
                            if (res.state.code === '0') {
                                this.$emit('ok')
                                this.$message.success(res.state.message)
                            } else {
                                this.$message.error(res.state.message)
                            }
                        }).finally(() => {
                            this.confirmLoading = false
                        })
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>

<style scoped>
    /deep/.avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    /deep/.avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    /deep/.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    /deep/.avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .edit-cont{
        box-sizing: border-box;
        padding: 20px;
        height: 300px;
        overflow-y: auto;
        border: 1px solid #E4E7ED;
        border-radius: 5px;
    }
</style>
